package com.axdoc.walletbiz.record.mq.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.axdoc.common.constant.MqConstant;
import com.axdoc.wallet.entity.MqRecords;
import com.axdoc.wallet.service.MqRecordsService;
import com.axdoc.walletbiz.record.mq.message.BizFrozenMessage;
import com.axdoc.walletbiz.record.mq.message.BizUnFrozenMessage;
import com.axdoc.walletbiz.record.mq.message.DkFailureMessage;
import com.axdoc.walletbiz.record.mq.message.DkSuccessMessage;
import com.axdoc.walletbiz.record.mq.message.Message;
import com.axdoc.walletbiz.record.mq.message.PaySuccessMessage;
import com.axdoc.walletbiz.record.mq.message.RefundSuccessMessage;
import com.axdoc.walletbiz.record.mq.message.RiskMessage;
import com.axdoc.walletbiz.record.mq.message.TransactionDoneMessage;
import com.axdoc.walletbiz.record.mq.message.WithdrawalCanMessage;
import com.axdoc.walletbiz.record.mq.message.WithdrawalIngMessage;
import com.axdoc.walletbiz.record.mq.service.MessageConvertService;

@Service("messageConvertService")
public class MessageConvertServiceImpl implements  MessageConvertService{
	public static Logger logger = LoggerFactory.getLogger("listen_message");

	public static Map<Integer, Class<? extends Message>> msgTypes = new HashMap<Integer, Class<? extends Message>>();
	
	@Autowired
	private MqRecordsService mqRecordsService;
	
	static {
		//支付成功消息处理
		msgTypes.put(MqConstant.PAY_SUCCESS, PaySuccessMessage.class);
		//退款成功消息处理
		msgTypes.put(MqConstant.REFUND_SUCCESS, RefundSuccessMessage.class);
		//风控消息处理
		msgTypes.put(MqConstant.RISK, RiskMessage.class);
		//业务消息处理-交易完成
		msgTypes.put(MqConstant.BIZ_DONE, TransactionDoneMessage.class);
		//业务消息处理-冻结
		msgTypes.put(MqConstant.BIZ_FROZEN, BizFrozenMessage.class);
		//业务消息处理-解冻
		msgTypes.put(MqConstant.BIZ_UNFROZEN, BizUnFrozenMessage.class);
		//业务消息处理-打款成功
		msgTypes.put(MqConstant.SETT_TRANSFER_SUCCESS, DkSuccessMessage.class);
		//业务消息处理-打款失败
		msgTypes.put(MqConstant.SETT_TRANSFER_FAILURE, DkFailureMessage.class);
		//业务消息处理-可提现
		msgTypes.put(MqConstant.SETT_WITHDRAWAL_CAN, WithdrawalCanMessage.class);
		//业务消息处理-提现中
		msgTypes.put(MqConstant.SETT_WITHDRAWAL_ING, WithdrawalIngMessage.class);
	}

	public Message convertToMessage(String message)  {
		int type=0;
		try {
			JSONObject jobject = JSON.parseObject(message);
			type = jobject.getIntValue("type");
			Message msg = JSON.parseObject(message, msgTypes.get(type));
			try{
				//调用账户服务-消息添加服务
	            MqRecords mqr = new MqRecords();
				mqr.setMsgType(String.valueOf(jobject.getIntValue("type")));
				mqr.setMsgFrom(String.valueOf(jobject.getString("from")));
				mqr.setMsgValue(jobject.getJSONObject("data").toJSONString());
				mqr.setHashCode(jobject.getJSONObject("data").toJSONString().hashCode());
				mqr.setState(MqConstant.MQ_STATUS_NOT_USED);
				mqr.setUniqueId(jobject.getString("id"));
	            int id = mqRecordsService.saveMqRecord(mqr);
	            msg.setId(String.valueOf(id));
	            logger.info("[消息落表成功，message=]"+message);
			}catch(Exception e){
				logger.info("[dqxx:消息落表异常，丢弃该条消息,message="+message+" ]");
				logger.info("[消息落表异常，]",e);
				return null;
			}
			return msg;
		} catch (Exception e) {
			logger.info("[消息工厂转换消息错误]",e);
		}
		return null;
	}
}
